param(
	[Parameter(Mandatory=$False)]
	[object]$vdi_list,						            # the vdi list
	[string]$check_name,                                # the process name to be checked
    [ValidateSet("service", "process","reboottime","loginuser")]    # check type
	[string]$check_type='process'
)
Switch -regex ($check_type){
    'process'{
        $check_action={
            param($line,$cnt,$process_name)
            try{$vdi,$domain,$fid,$env=$line.split(",").trim().ToUpper()}catch{}
            [array]$pros_result=$null
            If(([array]$rst=try{gwmi win32_process -ComputerName $vdi|?{$_.name.ToLower() -imatch ([regex]::new($process_name.ToLower()))} 2>$null}catch{}) -eq $null){
                $pros_result+=''|select @{n='VDI';e={$vdi}},@{n='ID';e={'n'}},@{n='Process';e={'n'}},@{n='Found';e={'n'}},@{n='StartTime';e={'n'}}
            }else{
                $rst|%{
                    $this_process=$_
                    $start=[datetime]::ParseExact($this_process.creationdate.Split('.')[0],"yyyyMMddHHmmss",$null)
                    $pros_result+=(''|select @{n='VDI';e={$vdi}},@{n='ID';e={$this_process.processid}},@{n='Process';e={$this_process.processname}},@{n='Found';e={'y'}},@{n='StartTime';e={$start}})
                }
            }
            return $pros_result
        }
    }
    'service'{
        $check_action={
            param($line,$cnt,$service_name)
            try{$vdi,$domain,$fid,$env=$line.split(",").trim().ToUpper()}catch{}
            [array]$service_result
            If(([array]$rst=get-service -cn $vdi -ea 0|?{$_.name.ToLower() -imatch ([regex]::new($service_name.ToLower())) } ) -eq $null){
                $service_result+=''|select @{n='VDI';e={$vdi}},@{n='Service';e={'n'}},@{n='Found';e={'n'}},@{n='Status';e={'n'}}
            }else{
                $rst|%{
                    $this_item=$_
                    $service_result+=(''|select @{n='VDI';e={$vdi}},@{n='Service';e={$this_item.name}},@{n='Found';e={'y'}},@{n='Status';e={$this_item.Status}})
                }
            }
            return $service_result
        }
    }
    "reboottime"{
        $check_action={
            param($vdi,$cnt,$service_name)
            try{if($vdi -like '*,*'){$vdi=$vdi.split(",").trim().ToUpper()[0]}}catch{}
            return [array](Get-WmiObject win32_operatingsystem -cn $vdi| select @{n='Env';E={$env}}, @{n='VDI';e={$vdi}}, @{n='LastBootUpTime';e={[datetime]::ParseExact($_.lastbootuptime.Split('.')[0],"yyyyMMddHHmmss",$null)}})
        }
    }
    "loginuser"{
        $check_action={
            param($vdi,$cnt,$service_name)
            try{if($vdi -like '*,*'){$vdi=$vdi.split(",").trim().ToUpper()[0]}}catch{}
            Get-Service RemoteRegistry -cn $vdi|?{$_.Status -ieq "Stopped"}|Start-Service
            Do{
                $rst=(Get-Service RemoteRegistry -cn $vdi).Status ; sleep -Milliseconds 200 
            }While( $rst -ine 'running' )
            reg add "\\$vdi\HKLM\SYSTEM\CurrentControlSet\Control\Terminal Server" /v AllowRemoteRPC /t Reg_Dword /d 0x1 /f 1>$null 2>&1
            If( ($user_rst= quser /server:$vdi 2>$null) -ne $null){ $loop_cnt=1
                $user_rst=$user_rst|%{
                    $this=$_ ; $this_length=$this.length
                    If( $this[23] -eq ' ' ){$this=$this.Substring(0,23)+'N/A'+ $this.Substring(24)}
                    # If( $this.Substring(54,9).trim() -eq ''){$this = $this.Substring(0,54)+"  N/A  " + $this.Substring(64)}
                    If( $loop_cnt -eq 1 ){ $vdi_header="VDI,"}else{$vdi_header="$vdi,"} $loop_cnt++
                    $vdi_header + ($this -replace '\s{2,}', ',')
                }| ConvertFrom-Csv
                return $user_rst
            }else{
                return (''|select @{n='VDI';E={$vdi}},@{n='USERNAME';E={'n'}},@{n='SESSIONNAME';E={'n'}},@{n='ID';E={'n'}},@{n='STATE';E={'n'}},@{n='IDLE TIME';E={'n'}},@{n='LOGON TIME';E={'n'}})
            }
        }
    }
}
$Global:check_result=Start-BulkThreads ($vdi_list.split("`n")|?{![string]::IsNullOrWhiteSpace($_.trim())}) $check_action ([string]::Join("|", ($check_name.split("`n")|?{![string]::IsNullOrWhiteSpace($_.trim())}))) |sort VDI,ID
